package com.hotel.front;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.CurrencyLogic;
import com.hotel.BL.GenericLogic;

import data.CurrencyMaster;
import data.General;
import data.passingData.ResultData;

@SuppressWarnings("serial")
public class CurrencyService  extends HttpServlet{
	// http://localhost:8080/currencyService?func=changeStatus&ChargeId=878
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String doClass = req.getParameter("func");
			if (doClass.equals("editUsingCurrency")) {
				EditUsingCurrency(req, resp);
			}else if(doClass.equals("getCurrency4Admin")){
				GetCurrency4Admin(req,resp);
			}else if(doClass.equals("editDefaultCurrency")){
				EditDefaultCurrency(req,resp);
			}else if(doClass.equals("delCurrency")){
				DeleteCurrency(req,resp);
			}else if(doClass.equals("getCurrencyForDropDown")){
				GetCurrencyForDropDown(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void EditUsingCurrency(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String id = req.getParameter("id");
		GenericLogic gLogic = new GenericLogic(req,resp);
		General g = gLogic.GetGeneral(hotelId);
		g.setUsingCurrencyString(id);
		
		gLogic.UpdateGenerlic(g);

		GetCurrency4Admin(req, resp);
	}

	private void EditDefaultCurrency(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		String id = req.getParameter("id");
		GenericLogic gLogic = new GenericLogic(req,resp);
		General g = gLogic.GetGeneral(hotelId);
		g.setDefaultCurrencyString(id);
		
		gLogic.UpdateGenerlic(g);

		GetCurrency4Admin(req, resp);
	}

	private void DeleteCurrency(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String id = req.getParameter("id");
		
		new CurrencyLogic(req,resp).DeleteCurrency(hotelId,id);
		
		GetCurrency4Admin(req, resp);
	}
	
	private void GetCurrency4Admin(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		double line = Double.parseDouble(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		
		List<CurrencyMaster> obj = new CurrencyLogic(req,resp).GetAllCurrency4AdminLimit(hotelId,offset,(long)line);
		long dataSize = new GenericLogic(req,resp).GetGeneral(hotelId).getUsingCurrency().size();
		String defaultCurrency = new GenericLogic(req,resp).GetGeneral(hotelId).getDefaultCurrencyString();
		
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("dataAmount",dataSize);
		data.put("pageNumber",pointer);
		data.put("lineAmount", line);
		data.put("pageAmount", Math.ceil(dataSize/line));
		data.put("data", obj);
		data.put("defaultCurrency", defaultCurrency);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
	
	private void GetCurrencyForDropDown(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{		
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<CurrencyMaster> usingCurrency = new GenericLogic(req,resp).GetGeneral(hotelId).getUsingCurrency();
		List<CurrencyMaster> allCurrency = new CurrencyLogic(req,resp).GetCurrencyRemain(usingCurrency);
		String defaultCurrency = new GenericLogic(req,resp).GetGeneral(hotelId).getDefaultCurrencyString();
		HashMap<String,Object> data = new HashMap<String,Object>();
		data.put("allCurrency",allCurrency);
		data.put("usingCurrency",usingCurrency);
		data.put("defaultCurrency",defaultCurrency);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(data));
	}
}
